﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using OpenGado.DATA.Entities;

namespace OpenGado.DATA
{

    /// <summary>
    /// Manages vendor information and contacts.
    /// </summary>
    public class VendorDataControl
    {

        private static VendorDataControl vendorDataControl;
        private static object  lockVendorControl = new object();

        private ISession session;

        /// <summary>
        /// Session handler.
        /// </summary>
        public ISession Session
        {

            get
            {

                session = FactoryHandler.CreateFactory().OpenSession();
                
                return session;
            }


        }

        /// <summary>
        /// Allows are to access the class without creating instance of it first, this is done in the get accessor.
        /// </summary>
        public static VendorDataControl Instance
        {
            get
            {
                lock (lockVendorControl)
                    if (vendorDataControl == null)
                    {
                        vendorDataControl = new VendorDataControl();
                    }

                return vendorDataControl;
            }
        }

        /// <summary>
        /// Saves vendor information
        /// </summary>
        /// <param name="vendor"></param>
        public void Save(Vendor vendor)
        {
            ISession tsession = Session;
            ITransaction transaction;

            try
            {
                transaction = tsession.BeginTransaction();
                tsession.SaveOrUpdate(vendor);
                transaction.Commit();
            }
            catch (Exception)
            {
                
                throw;
            }
            finally
            {
                tsession.Close();
            }


           
        }

        /// <summary>
        /// Returns a vendor object
        /// </summary>
        /// <param name="vendorId"></param>
        /// <returns></returns>
        public Vendor GetbyId(int vendorId)
        {
            ISession tsession = Session;
            Vendor vendor = tsession.Get<Vendor>(vendorId);
            tsession.Close();
            return vendor;
        }

        /// <summary>
        /// Returns a list of vendors
        /// </summary>
        /// <returns></returns>
        public  IList<Vendor> GetAllVendors()
        {
            ISession tsession = Session;
            ICriteria criteria = tsession.CreateCriteria<Vendor>();
            IList<Vendor> vendors = criteria.List<Vendor>();
            tsession.Close();
            return vendors;
        }

       

        /// <summary>
        /// Returns contact for the specified vendor
        /// </summary>
        /// <param name="vendorId"></param>
        /// <returns></returns>
        public IList<Contact> GetAllContacts (int vendorId)
        {
            
            ISession tsession = Session;
            ICriteria criteria = tsession.CreateCriteria<Contact>();
            criteria.Add(Restrictions.Eq("Vendor", vendorId));

            IList<Contact> contacts = criteria.List<Contact>();
            tsession.Close();

            return contacts;
       }


        public void Delete (Vendor v)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();
            tsession.Delete(v);
            transaction.Commit();

            tsession.Close();
        }
       
    }
}
